var NativeHttp = cc.Class({


    statics: {
        get: function (responseType, url, stateChangeCallback) {
            var xhr = new XMLHttpRequest();
            xhr.responseType = responseType;
            var time = false;//是否超时
            var self = this;
            var timer = setTimeout(function () {//做请求超时操作
                if (timer == null) {
                    return;
                }
                time = true;
                xhr.abort();//请求中止
                stateChangeCallback(true);
            }, 10000);
            xhr.onreadystatechange = function () {
                if (time){   //请求已经超时，忽略中止请求
                    return;
                }

                if (xhr.readyState == 4) {
                    clearTimeout(timer);//取消等待的超时
                    timer = null;
                    UIHelper.hideLoaing();
                    if (xhr.status != 200) {
                        Log.error(url+' - 请求回馈状态码为:', xhr.status)
                        let failMsg = HttpUtil.getTextWithStatus(xhr.status);
                        UIHelper.showMsgTips(failMsg,'网络错误','确定')
                       return;
                    }
                    stateChangeCallback(false, xhr);
                }
            }
            xhr.open('GET', encodeURI(url), true);//encodeURI(url)
            this.setHttpHeaderCookie(xhr);
            xhr.send();
            return xhr;
        },


        post: function (responseType,url, params, stateChangeCallback, isMustCallback) {
            // cc.log('----------------in sendPostResponese_url: ' + url);
            var xhr = new XMLHttpRequest();
            xhr.responseType = responseType||'arraybuffer';
            var time = false;//是否超时
            var self = this;
            var timer = setTimeout(function () {
                if (timer == null) {
                    return;
                }
                time = true;
                //做请求超时操作
                xhr.abort();//请求中止
                self.doTimeOut(url);
                stateChangeCallback(true);
            }, 10000);
            xhr.onreadystatechange = function () {
                if (time)//请求已经超时，忽略中止请求
                {
                    return;
                }
                if (xhr.readyState == 4) {
                    clearTimeout(timer);//取消等待的超时
                    timer = null;
                    UIHelper.hideLoaing();
                    if (xhr.status != 200) {
                        Log.error(url+' - 请求回馈状态码为:', xhr.status)
                        let failMsg = HttpUtil.getTextWithStatus(xhr.status);
                        UIHelper.showMsgTips(failMsg,'网络错误','确定')
                        return;
                    }
                    stateChangeCallback(false, xhr);
                }
            }
            xhr.open('POST', url, true);
            xhr.setRequestHeader("Content-Type", "application/x-protobuf");
            xhr.setRequestHeader("Accept", "application/x-protobuf");
            this.setHttpHeaderCookie(xhr);
            if (params) {
                xhr.send(params);
            }
            else {
                xhr.send();
            }
            return xhr;
        },

        setHttpHeaderCookie: function (xhr) {
            xhr.setRequestHeader("cookie", UserInfo.sessionId);
        },


    }
});

module.exports = NativeHttp;